#include "model.h"

void CModel::Draw()
{
	
	for( int j = 0; j < triangles.size(); j++ )
	{
		
		glBegin( GL_TRIANGLES );
			glColor4f( triangles[j].v[0].r, triangles[j].v[0].g, triangles[j].v[0].b, 1.0f ); glVertex3f( triangles[j].v[0].x, triangles[j].v[0].y, triangles[j].v[0].z );
			glColor4f( triangles[j].v[1].r, triangles[j].v[1].g, triangles[j].v[1].b, 1.0f ); glVertex3f( triangles[j].v[1].x, triangles[j].v[1].y, triangles[j].v[1].z );
			glColor4f( triangles[j].v[2].r, triangles[j].v[2].g, triangles[j].v[2].b, 1.0f ); glVertex3f( triangles[j].v[2].x, triangles[j].v[2].y, triangles[j].v[2].z );
		glEnd();
		
	}
	
}

void CModel::Load( const char * file )
{

	std::ifstream f( file );
bool b = false;
int lol = 0;
float r = 0.0f, g = 0.0f, bc = 0.0f;
	while( !f.eof() )
	{
		
	
		std::string t;
		
		f >> t;
		


		
		if( t.find( "triangles" ) != std::string::npos )
			b = true;
		
		if( t.find( "end" ) != std::string::npos && b )
			return;
		
		
		
		if( b )
		{
				
				if( t.find( ".tga" ) != std::string::npos )
				{
					
					MdlTriangle tri;
					
					for( int k = 0; k < 3; k++ )
					{
					
					int it;
					float x, y, z;
					char c = '\0';
					f >> it >> x >> y >> z;
					while( ( c = f.get() ) != '\r' && c != '\n' )
						continue;
				
					tri.v[k].x = x;
					tri.v[k].y = y;
					tri.v[k].z = z;
					tri.v[k].r = ( float )Util::random( 0, 255 ) / 255.0f;
					tri.v[k].g =  ( float )Util::random( 0, 255 ) / 255.0f;
					tri.v[k].b =  ( float )Util::random( 0, 255 ) / 255.0f;
					
					r += .0001f;
					g += .0002f;
					bc += .0003f;
					
					}
					
				triangles.push_back( tri );
					
					
				}
				
		}
		
			
		

	}

	f.close();

}